const add = (num1, num2) => {
    const len1 = num1.length;
    const len2 = num2.length;
    let maxLen = Math.max(len1, len2);
    let pre = 0;
    let result = '';
    for (let i = 0; i < maxLen; i++) {
        const sum = getNum(num1, i) + getNum(num2, i) + pre;
        pre = Math.floor(sum / 10);
        result = `${sum % 10}${result}`;
    }
    if (pre != 0) {
        result =`${pre}${result}`;
    }
    return threeSplit(result);
}

const threeSplit = (string) => {
    
    const length = string.length;
    let index = 0;
    let result = '';
    for (let i = length - 1; i >= 0; i--) {
        index++;
        result = `${string[i]}${result}`
        if (index === 3) {
            result = `,${result}`;
            index = 0;
        }
    }

    if (result[0] == ',') {
        return result.substring(1);
    }
    return result;
}

const getNum = (num, index) => {
    const len = num.length;
    if (index >= num.length) {
        return 0;
    }

    const number = num[len - index - 1];
    return parseInt(number);
}

console.log(add('111', '999'));
